package org.freeocs.commons.velocity;

import org.apache.commons.lang.StringUtils;

/**
 * 用于生成HTML标签的Toolbox类
 * 
 * @author chirs
 */
public class HtmlVelocityTool extends VelocityTool {
	public String image_src(String img_name) {
		StringBuffer img = new StringBuffer();
		img.append(super.root());
		img.append("/commons/images/");
		img.append(img_name);
		return img.toString();
	}

	public String img(String img_name) {
		StringBuffer prefix = new StringBuffer("<img src=\"");
		prefix.append(super.root());
		prefix.append("/commons/images/");
		prefix.append(img_name);
		prefix.append("\" border=\"0\" alt=\"\"/>");
		return prefix.toString();
	}

	public String img(String img_name, String alt) {
		StringBuffer prefix = new StringBuffer("<img src=\"");
		prefix.append(super.root());
		prefix.append("/commons/images/");
		prefix.append(img_name);
		prefix.append("\"");

		prefix.append(" alt=\"");
		prefix.append((alt == null) ? "" : alt);
		prefix.append("\"");

		prefix.append(" border=\"0\"");

		prefix.append("/>");
		return prefix.toString();
	}

	public String img(String img_name, String title, String alt, String align, int border) {
		StringBuffer prefix = new StringBuffer("<img src=\"");
		prefix.append(super.root());
		prefix.append("/commons/images/");
		prefix.append(img_name);
		prefix.append("\"");

		if (title != null) {
			prefix.append(" title=\"");
			prefix.append(title);
			prefix.append("\"");
		}

		prefix.append(" alt=\"");
		prefix.append((alt == null) ? "" : alt);
		prefix.append("\"");

		if (align != null) {
			prefix.append(" align=\"");
			prefix.append(align);
			prefix.append("\"");
		}

		prefix.append(" border=\"");
		prefix.append(border);
		prefix.append("\"");

		prefix.append("/>");
		return prefix.toString();
	}

	public String[] conntext(String content, String fix) {
		String[] args = new String[2];
		args[0] = "n";
		args[1] = "";
		if (StringUtils.isNotBlank(content)) {
			int pos = content.indexOf(fix);
			if (pos != -1) {
				args[0] = "y";
				args[1] = content.substring(0, pos);
			} else {
				args[1] = content;
			}
			return args;
		} else {
			return args;
		}
	}
}
